<?php

/**
 * LegendPie class
 *
 * Plot LegendPie
 *
 * @name LegendPie
 * @author DiMoN
 * @copyright Dmitry Pavlov
 */

class LegendPie extends Chart implements IChart
{
    const CHART_ID = "LegendPie",
    CHART_TYPE = 2,
    CHART_NAME = "Круговая диаграмма с легендой";

    /**
     * LegendPie constructor
     *
     */
    function __construct()
    {
        $this->isParamsValid();
    }

    /**
     * Gets chart ID specified by the const
     *
     * @return string
     */
    public static function getChartID()
    {
        return self::CHART_ID;
    }

    /**
     * Gets chart Type specified by the const
     *
     * @return int
     */
    public static function getChartType()
    {
        return self::CHART_TYPE;
    }

    /**
     * Gets chart Name specified by the const
     *
     * @return string
     */
    public static function getChartName()
    {
        return self::CHART_NAME;
    }

    /**
     * Checks the validity of the data arrays
     *
     */
    public function isParamsValid()
    {
        if (!isset(parent::$Data2D))
        {
            throw new Exception(self::CHART_ID . ": Data2D array is not set");
        }

        if (!isset(parent::$Labels))
        {
            throw new Exception(self::CHART_ID . ": Labels array is not set");
        }
    }

    /**
     * Plots chart
     *
     * @return string
     */
    public function plot()
    {
       	$c = new PieChart(parent::$Width, parent::$Height, parent::$Background);
       	$c->setRoundedFrame();
       	$c->setPieSize(parent::$Width / 2 - parent::$Width / 8, parent::$Height / 2, (parent::$Width + parent::$Height) / 10);
       	$c->set3D();
       	$c->addLegend(parent::$Width / 2 + parent::$Width / 4.5, 30);

       	$textBoxObj = $c->addTitle(parent::$Title, parent::$TitleFont, parent::$TitleFontSize, parent::$ColorTitle);
       	$textBoxObj->setBackground(parent::$BackgroundTitle1, parent::$BackgroundTitle2, glassEffect());

       	$c->setLabelFormat("{value} ({percent}%)");
        $c->setStartAngle(135);
         
       	/**
       	 * Adding data and labels
       	 */
       	$c->setData(parent::$Data2D, parent::$Labels);

        return parent::saveChart(&$c);
    }
}

?>
